import { ArgumentMetadata, HttpException, HttpStatus, Injectable, PipeTransform } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';

@Injectable()
export class PPipe implements PipeTransform {
    async transform(value: any, metadata: ArgumentMetadata) {
        const DTO = plainToInstance(metadata.metatype, value);
        
        const errors = await validate(DTO);
        if(errors.length > 0) {
            throw new HttpException(errors, HttpStatus.BAD_REQUEST);        
        }
        return value;
    }
}
